<?php
/**
 * Created by PhpStorm.
 * User: shrrung
 * Date: 2017/7/1
 * Time: 20:56
 */
namespace app\index\model;

use think\Db;
use think\Model;

class Comment extends Model
{

    protected $name = 'essay_comments';

    public static function self(){
        return new self();
    }

    public function delEssayCommentByEcId($where = []){
        if(empty($where)){
            return false;
        }
        return $this->where($where)->delete();
    }

    public function addEssayComment($data = []){
        if($data){
            $ec_id = db('essay_comments')->insertGetId($data);
            return $ec_id;
        }else{
            return false;
        }
    }

    public function modifyEssayCommentInfoIncOrDec($id = '',$field,$option = true)
    {
        if($option){
            $this->where('id',$id)->setInc($field);
        }else{
            $this->where('id',$id)->setDec($field);
        }


        return $this->where('id',$id)->find();
    }

    public function getCommentLikeInfo($where = []){
        return db('essay_comments_like_log')->where($where)->find();
    }

    public function essayLikeCommentLog($ec_id,$uid,$option)
    {
        db('essay_comments_like_log')->insert(['ec_id'=>$ec_id,'uid'=>$uid,'target_uid'=>0,'option'=>$option,'create_time'=>date('Y-m-d H:i:s',time())]);
    }

    public function delEssayLikeCommentLog($ec_id,$uid)
    {
        return db('essay_comments_like_log')->where(['ec_id'=>$ec_id,'uid'=>$uid])->delete();
    }

    public function getEssayCommentList($eid,$page = 1,$limit = 10)
    {
        return Db::view('essay_comments','id as ec_id,target_uid,content,like_count,create_time')
            ->view('user',['id'=>'uid','nickname'=>'nickname','avatar'=>'avatar'],'user.id=essay_comments.uid','LEFT')
            ->where(['essay_comments.eid'=>$eid,'essay_comments.status'=>1])
            ->order(['essay_comments.like_count'=>'desc','essay_comments.create_time'=>'desc'])
//            ->fetchSql(true)
            ->page($page)
            ->limit($limit)
            ->select();
    }

    public function getEssayCommentInfo($ec_id)
    {
        return Db::view('essay_comments','id as ec_id,target_uid,content,like_count,create_time')
            ->view('user',['id'=>'uid','nickname'=>'nickname','avatar'=>'avatar'],'user.id=essay_comments.uid','LEFT')
            ->where(['essay_comments.id'=>$ec_id,'essay_comments.status'=>1])
            ->order(['essay_comments.like_count'=>'desc','essay_comments.create_time'=>'desc'])
//            ->fetchSql(true)
            ->find();
    }

    public function getCommentInfo($ec_id){
        return db('essay_comments')->where(['ec_id'=>$ec_id])->find();
    }

    public function judgeEssayCommentIsLike($uid,$essay_comment_list)
    {
        if(empty($essay_comment_list)){
            return [];
        }else{
            if(is_mul_array($essay_comment_list)){
                foreach ($essay_comment_list as $key=>$essay) {
                    if(empty($uid)){
                        $essay_comment_list[$key]['is_like'] = 0;
                    }else{
                        $res = db('essay_comments_like_log')->where(['uid'=>$uid,'ec_id'=>$essay['ec_id']])->find();
                        if ($res) {
                            $essay_comment_list[$key]['is_like'] = 1;
                        } else {
                            $essay_comment_list[$key]['is_like'] = 0;
                        }
                    }

                }
                return $essay_comment_list;
            }else{
                if(empty($uid)){
                    $essay_comment_list['is_like'] = 0;
                }else{
                    $res = db('essay_comments_like_log')->where(['uid'=>$uid,'ec_id'=>$essay_comment_list['ec_id']])->find();
                    if($res){
                        $essay_comment_list['is_like'] = 1;
                    }else{
                        $essay_comment_list['is_like'] = 0;
                    }
                }

                return $essay_comment_list;
            }
        }

    }

}